package br.project.cdd.presentation;

import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.project.cdd.bean.UserBean;
import br.project.cdd.vo.UserVO;

public class LoginServlet extends HttpServlet {
	
	
	@Override
	public void service (HttpServletRequest request, HttpServletResponse response){
		
		String usuario = request.getParameter("txtUser");
		String senha = request.getParameter("txtSenha");
		
		try{
			if (usuario == "" || senha == ""){
				HttpSession session = request.getSession();
				session.setAttribute("er", "1");
				response.sendRedirect("login.jsp");
			}
			else{
				UserBean bean = new UserBean();
				bean.setNmUsuario(usuario);
				bean.setSenha(senha);
				
				UserVO usrVo = new UserVO();
				UserBean user = usrVo.buscarUsuario(bean);
				if (user == null){
					HttpSession session = request.getSession();
					session.setAttribute("er", "2");
					response.sendRedirect("login.jsp");
				}
				else{
					HttpSession session = request.getSession();
					session.removeAttribute("er");
					response.sendRedirect("index.jsp?user=" + user.getIdUsuario());					
				}
			}
		}	
		catch(IOException io){
			io.printStackTrace();
		}
	}
}
